package vip.housir.kotlindemo.service.impl

import org.springframework.cache.annotation.CacheConfig
import org.springframework.cache.annotation.CacheEvict
import org.springframework.cache.annotation.Cacheable
import javax.annotation.Resource
import org.springframework.stereotype.Service
import vip.housir.kotlindemo.entity.User
import vip.housir.kotlindemo.mapper.UserMapper
import vip.housir.kotlindemo.service.UserService
import java.util.Date

@Service
@CacheConfig(cacheNames = ["User"])
class UserServiceImpl : UserService {
    @Resource
    lateinit var userMapper: UserMapper

    override fun createUser(user: User) {
        user.createAt = Date()
        userMapper.insert(user)
    }

    @Cacheable(key = "#p0", unless = "#result==null")
    override fun findUserById(userId: Long?): User? {
        return userMapper.selectByPrimaryKey(userId)
    }

    @CacheEvict(key = "#p0", allEntries = true)
    override fun updateUser(user: User) {
        user.updateAt = Date()
        userMapper.updateByPrimaryKeySelective(user)
    }

    @CacheEvict(key = "#p0", allEntries = true)
    override fun deleteUserById(userId: Long?): Int {
        return userMapper.deleteByPrimaryKey(userId)
    }
}
